<template>
  <div class="from">
    <div class="boxf">
      <span>Cancer Type</span>
      <el-select
        @change="xios"
        v-model="cancerType"
        placeholder="Cancer Type"
        style="width: 240px"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </div>
    <div class="boxf">
      <span>Methiod</span>
      <el-select
        @change="xios"
        v-model="MethiodType"
        placeholder="Select"
        style="width: 240px"
      >
        <el-option
          v-for="item in MethiodOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </div>

    <div class="boxf">
      <span>Database</span>
      <el-select
        @change="xios"
        v-model="databaseType"
        placeholder="Select"
        style="width: 240px"
      >
        <el-option
          v-for="item in databaseOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </div>

    <div class="boxf">
      <span>Cell Visualizaton</span>
      <el-select
        @change="getX"
        v-model="xType"
        placeholder="Select"
        style="width: 240px"
      >
        <el-option
          v-for="item in XOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </div>
    <div class="boxf">
      <span>Pathway Visulization</span>
      <el-select
        @change="getY"
        v-model="yType"
        placeholder="Select"
        style="width: 240px"
      >
        <el-option
          v-for="item in YOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </div>
  </div>
  <div
    style="
      width: 100%;
      padding: 15px;
      display: flex;
      background-color: rgb(251, 251, 251);
    "
  >
    <div @mouseleave="moren" class="bigbox">
      <div class="boxs" v-for="(iten, index) in data.data" :key="iten">
        <el-tooltip
          class="item"
          effect="dark"
          :content="iten.name"
          placement="top-start"
        >
          <div class="boxd" @mouseenter="xdefault">
            <p>{{ iten.name }}</p>
            <div class="ruifours"></div>
          </div>
        </el-tooltip>
        <div class="right" style="" :class="{shangtop: index == 0}">
          <div style="display: flex">
            <div
              @mouseenter="Hit(iten.name, index)"
              :style="{
                width: 30 + 'px',
              }"
              style="margin: 0 0px"
              class="lOne"
              v-for="(item, index) in iten.list"
              :key="item"
            >
              <!-- <el-tooltip
                class="item"
                effect="dark"
                :content="item"
                placement="top-start"
              >
                <div
                  class="yuan"
                  :style="{
                    width: (item > 0.1 ? item : 0.06 * 2) * 20 + 'px',
                    height: (item > 0.1 ? item : 0.06 * 2) * 20 + 'px',
                    borderRadius: '50%',
                    backgroundColor:
                      item > 0.7
                        ? `rgb(${(item ? item : 0.1) * 255}, ${210},
                  ${255 / ((item ? item : 0.1) * 100)})`
                        : item > 0.4
                        ? `rgb(0, 189,
                      ${(item ? item : 0.1) * 255})`
                        : `rgb(0, ${(item ? item : 0.1) * 255},
                  126)`,
                  }"
                ></div>
              </el-tooltip> -->
              <div
                class="yuan"
                :style="{
                  width: (item > 0.1 ? item : 0.06 * 2) * 20 + 'px',
                  height: (item > 0.1 ? item : 0.06 * 2) * 20 + 'px',
                  borderRadius: '50%',
                  backgroundColor:
                    item > 0.7
                      ? `rgb(${(item ? item : 0.1) * 255}, ${210},
                  ${255 / ((item ? item : 0.1) * 100)})`
                      : item > 0.4
                      ? `rgb(0, 189,
                      ${(item ? item : 0.1) * 255})`
                      : `rgb(0, ${(item ? item : 0.1) * 255},
                  126)`,
                }"
              ></div>
              <div class="div-titile">
                {{ iten.name }}
                <br />{{ item }}
                <br />
                {{ data.xData[index] }}
              </div>
            </div>
          </div>
          <p class="line" v-show="index == 0"></p>
        </div>
      </div>
      <div class="zhns" style="display: flex">
        <div class="zhanwi"></div>
        <div v-for="item in data.xData" :key="item" class="botom">
          <el-tooltip
            class="item"
            effect="dark"
            :content="item"
            placement="top-start"
          >
            <p class="zit">{{ item }}</p>
          </el-tooltip>
        </div>
      </div>
    </div>
    <div class="rule">
      <div class="bg">
        <p>value</p>

        <div class="rus">
          <div class="zhi"></div>
          <div class="zhion"></div>
          <div class="zhions"></div>
          <div>
            <span class="ruwo">0.75</span>
          </div>
          <div>
            <span class="ruwo sd">0.50</span>
          </div>
          <div>
            <span class="ruwo sds">0.25</span>
          </div>
          <div>
            <span class="ruwo sdss">0.00</span>
          </div>
          <span class="ruwo sss">1.00</span>
        </div>
      </div>
      <!-- <div>
        <p class="bg">value</p>
        <div class="sdf">
          <div class="zezo"></div>
          <div class="zezoo"></div>
          <div class="zezooo"></div>
          <div class="zezoooo"></div>
          <div class="zezooooo"></div>
        </div>
      </div> -->
    </div>
    <div class="boxright">
      <div>
        <p class="title">cell</p>
        <div class="cell">
          <dot
            @click="dialogVisible = true"
            v-if="chuant.length && !suo && xType !== 'Cell'"
            :data="chuant"
            :yuan="4"
            :width="500"
            :height="300"
            :fons="4"
          />
          <el-image
            v-else
            style="width: 100%; height: 100%"
            :src="ximg"
            :preview-src-list="srcList"
          >
            <template #error>
              <div class="image-slot">
                Please select a pathway on the left to visualize its
                microenvironment activity
              </div>
            </template>
          </el-image>
        </div>
      </div>
      <div>
        <p class="title">path</p>
        <div class="cell">
          <el-image
            style="width: 100%; height: 100%"
            :src="yurl"
            :preview-src-list="srcLists"
          >
            <template #error>
              <div class="image-slot">
                Please select a pathway on the left to visualize its
                microenvironment activity
              </div>
            </template>
          </el-image>
        </div>
      </div>
    </div>
    <el-dialog
      v-model="dialogVisible"
      width="95%"
      center
      :before-close="handleClose"
    >
      <div style="width: 100%; display: flex; justify-content: center">
        <dot
          v-if="chuan.length && !suo && xType !== 'Cell'"
          :data="chuan"
          :width="1200"
          :height="1000"
          :yuan="8"
          :fons="10"
        />
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false">
            Cancel
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import dot from "./dot.vue";
import {getData} from "../../api/index";
import Papa from "papaparse";
import dds from "./dd.vue";
import {jcancerType} from "../dta/cancerTye";
import {ref, onMounted, reactive, onUpdated} from "vue";
const dialogVisible = ref(false);
let xurl = ref("");
let srcList = ref([]);
let srcLists = ref([]);
let cancerType = ref(
  window.sessionStorage.getItem("cancerType")
    ? window.sessionStorage.getItem("cancerType")
    : "SKCM"
);
let MethiodType = ref("AUCell");
let databaseType = ref("GO");
let xType = ref("Cell");
let yType = ref("Dimplot");
let ximg = ref("");
let yurl = ref("");
let xpngName = ref();
let ypngName = ref("");
let suo = ref(true);
let yimg = ref("");
let xmo = ref("");
let chuan = ref([]);
let jie = ref(true);
let timer = ref();
let chuant = ref([]);
const moren = () => {
  xdefault();
};

const xdefault = () => {
  suo.value = false;
  xmo.value = `/Umap/${cancerType.value}/${cancerType.value}.png`;

  if (xType.value == "UMAP") {
    xmo.value = `/${cancerType.value}/${MethiodType.value}/${databaseType.value}/${cancerType.value}_${databaseType.value}Path_Umap/Pathloc.csv`;
    chuan.value = [];
    chuant.value = [];
    getData(xmo.value).then((res) => {
      Papa.parse(res, {
        header: true, // 指定第一行为标题行
        dynamicTyping: true, // 自动转换数据类型
        complete: function (results) {
          // 处理解析完成的结果
          const data = results.data
            .map((row, index) => {
              if (index === 0) return null; // 跳过标题行

              return {
                pathway: row.pathway,
                x: parseFloat(row.UMAP_1 * 50), // 将字符串转换为浮点数
                y: parseFloat(row.UMAP_2 * 70), // 将字符串转换为浮点数
              };
            })
            .filter((item) => item !== null && item.pathway !== undefined); // 过滤掉标题行后的无效数据

          const datas = results.data
            .map((row, index) => {
              if (index === 0) return null; // 跳过标题行

              return {
                pathway: row.pathway,
                x: parseFloat(row.UMAP_1 * 20), // 将字符串转换为浮点数
                y: parseFloat(row.UMAP_2 * 30), // 将字符串转换为浮点数
              };
            })
            .filter((item) => item !== null && item.pathway !== undefined); // 过滤掉标题行后的无效数据

          chuan.value = data;
          chuant.value = datas;
          // 这里可以将 data 传递给其他函数或者保存到状态中
        },
      });
    });
  } else {
    getData(xmo.value).then((res) => {
      ximg.value = res;
      srcList.value[0] = res;
    });
  }
};
const xios = () => {
  ximg.value = "";
  suo.value = false;
  yurl.value = "";
  xpngName.value = "";
  ypngName.value = "";
  xdefault();
  getData(
    `/${cancerType.value}/${MethiodType.value}/${databaseType.value}/${cancerType.value}_${databaseType.value}Dotplot/wide_matrix.csv`
  ).then((res) => {
    Papa.parse(res, {
      complete: (results) => {
        const headers = results.data[0]; // 获取表头
        // results.data 是一个二维数组，每个元素是一个对象
        const parsedData = results.data.slice(1).map((row) => {
          return {
            name: row[0],
            list: row.slice(1).map(parseFloat),
          };
        });
        data.data = parsedData.splice(0, parsedData.length - 1);
        data.xData = headers.splice(1, headers.length - 1);
      },
      header: false,
    });
  });
};

const Hit = (item, index) => {
  suo.value = true;
  ypngName.value = item;
  if (jie.value) {
    jie.value = false;
    timer.value = setTimeout(() => {
      xpngName.value = data.xData[index];
      getX();
      getY();
      jie.value = true;
    }, 0);
  }
};

const getY = () => {
  yimg.value = `/${cancerType.value}/${MethiodType.value}
/${databaseType.value}/${cancerType.value}_${databaseType.value}${yType.value}/${ypngName.value}.png`;

  getData(yimg.value).then((res) => {
    yurl.value = res;
    srcLists.value[0] = res;
  });
};
const getX = () => {
  if (suo.value) {
    if (xType.value == "Cell") {
      xurl.value = `/Umap/${cancerType.value}/${xpngName.value}.png`;
      // xurl.value = `/${cancerType.value}/Cell/${xpngName.value}.png`;
    } else if (xType.value == "UMAP") {
      xurl.value = `/${cancerType.value}/${MethiodType.value}/${databaseType.value}/${cancerType.value}_${databaseType.value}Path_Umap/${xpngName.value}.png`;
    }

    getData(xurl.value).then((res) => {
      ximg.value = res;
      srcList.value[0] = res;
      console.log(srcList);
    });
  } else {
    xdefault();
  }
};
xios();

let YOptions = [
  {
    value: "Dimplot",
    label: "Dimplot",
  },
  {
    value: "Boxplot",
    label: "Boxplot",
  },
];
let XOptions = [
  {
    value: "Cell",
    label: "Cell",
  },
  {
    value: "UMAP",
    label: "UMAP",
  },
];
let databaseOptions = [
  {
    value: "GO",
    label: "GO",
  },
  {
    value: "HMDB",
    label: "HMDB",
  },
  {
    value: "KEGG",
    label: "KEGG",
  },
  {
    value: "Reactome",
    label: "Reactome",
  },
  {
    value: "Hallmark",
    label: "Hallmark",
  },
];

let options = jcancerType;
let MethiodOptions = [
  {
    value: "AUCell",
    label: "AUCell",
  },
  {
    value: "ssGSEA",
    label: "ssGSEA",
  },
  {
    value: "VISION",
    label: "VISION",
  },
];

let data = reactive({
  data: [],
  xData: [],
});
let a = new URL("../../assets/mo.png", import.meta.url).href;
const err = (e) => {
  e.target.src = a;
};
</script>
<style scoped>
.image-slot {
  width: 100%;
  height: 100%;
  align-items: center;
  color: var(--el-text-color-placeholder);
  display: flex;
  font-size: 14px;
  justify-content: center;
  vertical-align: middle;
  color: var(--el-text-color-placeholder);
  background: #f5f7fa;
}
.boxf {
  margin: 20px;
  text-align: left;
}
.el-select {
  width: 180px !important;
}
.from {
  display: flex;
}
.from span {
  font-weight: 700;
  font-size: 16px;
  margin-right: 5px;
}
.zezooooo {
  position: relative;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: rgb(0, 26, 126);
}
.zezooooo::before {
  position: absolute;
  right: -30px;
  top: 1px;
  content: "1.00";
  font-size: 12px;
  font-weight: 700;
}
.zezoooo {
  position: relative;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: rgb(0, 26, 126);
}

.zezoooo::before {
  position: absolute;
  right: -33px;
  top: -0px;
  content: "0.75";
  font-size: 12px;
  font-weight: 700;
}

.zezooo {
  position: relative;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: rgb(0, 26, 126);
}
.zezooo::before {
  position: absolute;
  right: -36px;
  top: -3px;
  content: "0.50";
  font-size: 12px;
  font-weight: 700;
}
.zezoo {
  position: relative;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: rgb(0, 26, 126);
}
.zezo {
  position: relative;
  width: 2.5px;
  height: 2.5px;
  border-radius: 50%;
  background-color: rgb(0, 26, 126);
}
.zezoo::before {
  position: absolute;
  right: -39px;
  top: -6px;
  content: "0.25";
  font-size: 12px;
  font-weight: 700;
}
.zezo::before {
  position: absolute;
  right: -40px;
  top: -8px;
  content: "0.00";
  font-size: 12px;
  font-weight: 700;
}
.sdf {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  width: 20px;
  height: 100px;
  margin-left: 5px;
}
.bg {
  font-size: 12px;
  font-weight: 700;
  margin-top: 50px;
  margin-bottom: 5px;
}

.zhions::before {
  position: absolute;
  top: 75px;
  content: "";
  display: inline-block;
  width: 5px;
  height: 2px;
  background-color: #ffffff;
}
.zhion::before {
  position: absolute;
  top: 50px;
  content: "";
  display: inline-block;
  width: 5px;
  height: 2px;
  background-color: #ffffff;
}
.zhions::after {
  position: absolute;
  top: 75px;
  right: 0;
  content: "";
  display: inline-block;
  width: 5px;
  height: 2px;
  background-color: #ffffff;
}
.zhion::after {
  position: absolute;
  top: 50px;
  right: 0;
  content: "";
  display: inline-block;
  width: 5px;
  height: 2px;
  background-color: #ffffff;
}
.zhi::before {
  position: absolute;
  top: 24px;
  content: "";
  display: inline-block;
  width: 5px;
  height: 2px;
  background-color: #ffffff;
}

.zhi::after {
  position: absolute;
  top: 24px;
  right: 0;
  content: "";
  display: inline-block;
  width: 5px;
  height: 2px;
  background-color: #ffffff;
}
.ruwo {
  position: absolute;
  top: 16px;
  right: -30px;
}
.sds {
  top: 67px;
}
.sd {
  top: 42px;
}
.sdss {
  top: 88px;
}
.sss {
  top: -7px;
}
.rus {
  position: relative;
  width: 20px;
  margin-left: 5px;
  height: 100px;
  border-radius: 2px;
  background: linear-gradient(
    to top,
    rgb(0, 44, 126),
    rgb(0, 77, 126),
    rgb(0, 189, 153),
    rgb(204, 255, 3),
    rgb(255, 210, 3)
  );
}
.lOne {
  cursor: pointer;

  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
/* .line {
  background: rgb(red, green, blue);
  position: absolute;
  width: calc(100% - 2%);
  margin-top: -1px;
  margin-left: 10px;
  height: 1px;
  background-color: #000;
} */
.boxright {
  flex-shrink: 0;
  margin-left: 150px;
  padding: 5px;
  border-radius: 10px;
  background: #fff;
}
.title {
  font-size: 24px;
}
.cell {
  width: 500px;
  height: 300px;
  border: 1px solid #000;
}
.rule {
  margin-left: 10px;
  flex-shrink: 0;
}
.bigbox {
  flex-shrink: 0;
  height: 650px;
  width: 720px;
  padding: 10px;
  border-radius: 10px;
  padding-bottom: 0;
  background: #fff;
  overflow: scroll;
}
.bigbox::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 5px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 5px;
}
.zit {
  position: absolute;
  top: 5px;
  background: #fff;

  left: -192px;
  width: 200px;
  text-align: right;
  transform-origin: right top;
  transform: rotate(-45deg);
  word-break: keep-all;

  white-space: nowrap;

  overflow: hidden;

  text-overflow: ellipsis;
}

.zhns {
  position: sticky;
  bottom: 0;
  margin-left: -20px;
  justify-content: flex-end; /* 主轴（水平方向）靠右对齐 */
  height: 150px;
  background: #fff;
  z-index: 999;
}
.botom {
  position: relative;
  background: #fff;
  flex-shrink: 0;
  width: 30px;
  height: 1px;
  padding: 0 10px;

  font-size: 12px;
  background-color: #000;
}
.botom::before {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  content: "";
  display: inline-block;
  height: 4px;
  width: 1px;
  background: #000;
}
.boxs {
  justify-content: flex-end; /* 主轴（水平方向）靠右对齐 */
  display: inline-block;
  display: flex;
  margin-top: -1px;
  /* border-right: 1px solid #000; */
}
.zhanwi {
  position: sticky;
  left: 0;
  z-index: 999;
  background: #fff;
  text-align: right;
  line-height: 30px;
  max-width: 350px;
  height: 10px;
  font-size: 12px;
  flex-shrink: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.boxd {
  display: flex;
  left: 0;
  z-index: 999;
  background: #fff;
  text-align: right;
  line-height: 30px;
  max-width: 350px;
  font-size: 12px;
  flex-shrink: 0;

  /* width: 300px; */
}
.boxd p {
  max-width: 340px;
  padding-right: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.right {
  display: flex;
  /* background: rgb(212, 212, 242); */
}

.shangtop {
  /* border-top: 1px solid #000; */
}
.ruifours {
  float: right;
  flex-shrink: 0;
  position: relative;
  height: 30px;
  width: 1px;
  left: -5px;
  margin-left: 10px;
  margin-right: -5px;
  background-color: #000;
}
.yuan {
  position: relative;
  filter: blur(0.5px);
}
.lOne:hover .yuan {
  background: #66ecea !important;
}
.div-titile {
  position: absolute;
  top: 30px;
  padding: 3px;
  white-space: nowrap;
  z-index: 99999;
  background: #000;
  color: #fff;
  text-align: center;
  font-size: 12px;
  display: none;
}
.lOne:hover .div-titile {
  display: block;
}
.ruifours::before {
  position: absolute;
  left: -4px;
  top: 50%;
  transform: translateY(-50%);
  content: "";
  display: inline-block;
  width: 4px;
  height: 1px;
  background: #000;
}
</style>
